﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Data;


public partial class Admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindVendor();
            ViewState["VendorContactID1"] = null;
            ViewState["VendorContactID2"] = null;

        }
    }

    private void BindVendor()
    {
        var VendorList = new DealesDataContext();
        var Vendor = VendorList.Vendor_Return();
        RadGrid1.DataSource = Vendor;
        RadGrid1.DataBind();
    }

    //protected void dropCity_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    //{

    //    GridEditableItem editItem = (GridEditableItem)((RadComboBox)sender).NamingContainer;
    //    RadComboBox dropCity = editItem.FindControl("dropCity") as RadComboBox;
    //    dropCity.Items.Clear();
    //    var deal = new DealesDataContext();
    //    var City = deal.City_Return();
    //    foreach (var item in City)
    //    {

    //        RadComboBoxItem Raditem = new RadComboBoxItem();
    //        Raditem.Text = item.City.ToString();
    //        Raditem.Value = item.CityID.ToString();
    //        Raditem.Attributes.Add("City", item.City.ToString());
    //        dropCity.Items.Add(Raditem);
    //        Raditem.DataBind();
    //    }
    //}
    //protected void RadComboBox2_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    //{

    //    GridEditableItem editItem = (GridEditableItem)((RadComboBox)sender).NamingContainer;
    //    RadComboBox RadComboBox2 = editItem.FindControl("RadComboBox2") as RadComboBox;
    //    RadComboBox2.Items.Clear();
    //    var deal = new DealesDataContext();
    //    var City = deal.City_Return();
    //    int itemOffset = e.NumberOfItems;
    //    foreach (var item in City)
    //    {
    //        RadComboBox2.Items.Add(new RadComboBoxItem(item.City.ToString(), item.CityID.ToString()));
    //    }
    //}
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        try
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                Label Address1 = (Label)e.Item.FindControl("lblAddress1");
                Label Address2 = (Label)e.Item.FindControl("lblAddress2");
                Label City = (Label)e.Item.FindControl("lblCity");
                Address1.Text = "<b>Address1 :</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Address1"));
                Address2.Text = "<b>Address2 :</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Address2"));
                City.Text = "<b>City :</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "City")) + " <b>State :</b>" + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "State")) + " <b>ZipCode :</b> " + Convert.ToString(DataBinder.Eval(e.Item.DataItem, "ZipCode"));


            }
        }
        catch
        {
        }
    }
    //public void dropCity_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    //{
    //    GridEditableItem editItem = (GridEditableItem)((RadComboBox)sender).NamingContainer;
    //    TextBox txtCity = editItem.FindControl("txtCity") as TextBox;
    //    txtCity.Text = "";
    //}

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        try
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                ViewState["VendorContactID1"] = null;
                ViewState["VendorContactID2"] = null;

                //RadComboBox dropCity = e.Item.FindControl("dropCity") as RadComboBox;

                var deal = new DealesDataContext();
                //var City1 = deal.Select_City();

                ////dropCity.DataTextField = "City";
                ////dropCity.DataValueField = "CityID";
                ////dropCity.DataSource = City1;
                ////dropCity.DataBind();
                ////dropCity.Items.Insert(0, new RadComboBoxItem("- Select a continent -"));
                ////dropCity.SelectedIndex = 0;
                //dropCity.Items.Add(new RadComboBoxItem("Select City", "0"));
                //foreach (var item in City1)
                //{
                //    dropCity.Items.Add(new RadComboBoxItem(item.City.ToString(), item.CityID.ToString()));
                //}


                RadComboBox dropIndustry = e.Item.FindControl("dropIndustry") as RadComboBox;
                var Category = deal.Industry_Return();
                dropIndustry.DataTextField = "Industry";
                dropIndustry.DataValueField = "IndustryID";
                dropIndustry.DataSource = Category;
                dropIndustry.DataBind();



                TextBox txtPhnoeNo = e.Item.FindControl("txtPhnoeNo") as TextBox;
                var editableItem = ((GridEditableItem)e.Item);
                var VendorID = (long)editableItem.GetDataKeyValue("VendorID");
                var VendorInfo = new DealesDataContext();
                var query1 = VendorInfo.Select_VendorByID(Convert.ToInt32(VendorID));
                foreach (var item in query1)
                {

                    //dropCity.SelectedValue = item.CityID.ToString();
                    dropIndustry.SelectedValue = item.IndustryID.ToString();
                    txtPhnoeNo.Text = item.Phone_Off_.ToString();
                }
                TextBox txtContactFName1 = e.Item.FindControl("txtContactFName1") as TextBox;
                TextBox txtContactLName1 = e.Item.FindControl("txtContactLName1") as TextBox;
                TextBox txtContactMInitial1 = e.Item.FindControl("txtContactMInitial1") as TextBox;
                TextBox txtContactEmail1 = e.Item.FindControl("txtContactEmail1") as TextBox;
                TextBox txtContactPhoneNum1 = e.Item.FindControl("txtContactPhoneNum1") as TextBox;
                TextBox txtContactFax1 = e.Item.FindControl("txtContactFax1") as TextBox;
                TextBox txtAddress1 = e.Item.FindControl("txtAddress1") as TextBox;
                TextBox txtAddress2 = e.Item.FindControl("txtAddress2") as TextBox;
                TextBox txtContactFName2 = e.Item.FindControl("txtContactFName2") as TextBox;
                TextBox txtContactPhoneNum2 = e.Item.FindControl("txtContactPhoneNum2") as TextBox;
                TextBox txtContactFax2 = e.Item.FindControl("txtContactEmail2") as TextBox;
                TextBox txtContactLName2 = e.Item.FindControl("txtContactLName2") as TextBox;
                TextBox txtContactMInitial2 = e.Item.FindControl("txtContactMInitial2") as TextBox;
                TextBox txtContactEmail2 = e.Item.FindControl("txtContactEmail2") as TextBox;

                int i = 0;

                var query2 = VendorInfo.Select_VendorContactByVendorID(Convert.ToInt32(VendorID));

                foreach (var item in query2)
                {
                    if (i == 0)
                    {
                        txtContactEmail1.Text = item.ContactEmail.ToString();
                        txtContactFax1.Text = item.Fax.ToString();
                        txtContactFName1.Text = item.ContactFName.ToString();
                        txtContactLName1.Text = item.ContactLName.ToString();
                        txtContactMInitial1.Text = item.ContactMinitial.ToString();
                        txtContactPhoneNum1.Text = item.ContactPhone.ToString();
                        ViewState["VendorContactID1"] = item.VendorContactID.ToString();
                        i++;
                    }
                    else
                    {
                        txtContactEmail2.Text = item.ContactEmail.ToString();
                        txtContactFax2.Text = item.Fax.ToString();
                        txtContactFName2.Text = item.ContactFName.ToString();
                        txtContactLName2.Text = item.ContactLName.ToString();
                        txtContactMInitial2.Text = item.ContactMinitial.ToString();
                        txtContactPhoneNum2.Text = item.ContactPhone.ToString();
                        ViewState["VendorContactID2"] = item.VendorContactID.ToString();
                    }
                }
            }
        }
        catch
        {
        }
    }


    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        try
        {
            var VendorList = new DealesDataContext();
            var Vendor = VendorList.Vendor_Return();
            RadGrid1.DataSource = Vendor;
        }
        catch
        {
        }
    }

    protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
    {
        try
        {
            var VendorID = (long)((GridDataItem)e.Item).GetDataKeyValue("VendorID");
            var Vendor = new DealesDataContext();
            var query = Vendor.Delete_Vendor(VendorID);
        }
        catch
        {
        }
    }

    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    {
        try
        {

            RadComboBox dropIndustry = e.Item.FindControl("dropIndustry") as RadComboBox;          
            TextBox txtName = e.Item.FindControl("txtName") as TextBox;         
            TextBox txtZipCode = e.Item.FindControl("txtZipCode") as TextBox;
            TextBox txtContactFName1 = e.Item.FindControl("txtContactFName1") as TextBox;
            TextBox txtContactLName1 = e.Item.FindControl("txtContactLName1") as TextBox;
            TextBox txtContactMInitial1 = e.Item.FindControl("txtContactMInitial1") as TextBox;
            TextBox txtContactEmail1 = e.Item.FindControl("txtContactEmail1") as TextBox;
            TextBox txtContactPhoneNum1 = e.Item.FindControl("txtContactPhoneNum1") as TextBox;
            TextBox txtContactFax1 = e.Item.FindControl("txtContactFax1") as TextBox;
            TextBox txtAddress1 = e.Item.FindControl("txtAddress1") as TextBox;
            TextBox txtAddress2 = e.Item.FindControl("txtAddress2") as TextBox;
            TextBox txtContactFName2 = e.Item.FindControl("txtContactFName2") as TextBox;
            TextBox txtState = e.Item.FindControl("txtState") as TextBox;
            TextBox txtContactLName2 = e.Item.FindControl("txtContactLName2") as TextBox;
            TextBox txtContactMInitial2 = e.Item.FindControl("txtContactMInitial2") as TextBox;
            TextBox txtContactEmail2 = e.Item.FindControl("txtContactEmail2") as TextBox;
            TextBox txtPhnoeNo = e.Item.FindControl("txtPhnoeNo") as TextBox;
            TextBox txtWebSite = e.Item.FindControl("txtWebSite") as TextBox;
            TextBox txtFax = e.Item.FindControl("txtFax") as TextBox;
            TextBox txtEmail = e.Item.FindControl("txtEmail") as TextBox;
            TextBox txtContactPhoneNum2 = e.Item.FindControl("txtContactPhoneNum2") as TextBox;
            TextBox txtContactFax2 = e.Item.FindControl("txtContactEmail2") as TextBox;
            TextBox txtCity = e.Item.FindControl("txtCity") as TextBox;
            Label lblmessage = e.Item.FindControl("lblmessage") as Label;
            var VendorInsert = new DealesDataContext();
            var Vendor = VendorInsert.Vendor_Insert(Convert.ToInt32(dropIndustry.SelectedValue), txtName.Text, txtAddress1.Text, txtAddress2.Text,txtCity.Text, txtState.Text, txtZipCode.Text, txtPhnoeNo.Text, txtFax.Text, txtWebSite.Text, txtEmail.Text, Convert.ToBoolean(0), txtContactFName1.Text, txtContactLName1.Text, txtContactMInitial1.Text, txtContactEmail1.Text, txtContactPhoneNum1.Text, txtContactFax1.Text, txtContactFName2.Text, txtContactLName2.Text, txtContactMInitial2.Text, txtContactEmail2.Text, txtContactPhoneNum2.Text, txtContactFax2.Text);
            Response.Redirect("Vendor.aspx");

        }
        catch
        {
        }
    }
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {

        try
        {

            var editableItem = ((GridEditableItem)e.Item);
            var VendorID = (long)editableItem.GetDataKeyValue("VendorID");
            RadComboBox dropIndustry = e.Item.FindControl("dropIndustry") as RadComboBox;
           
            TextBox txtName = e.Item.FindControl("txtName") as TextBox;
          
            TextBox txtZipCode = e.Item.FindControl("txtZipCode") as TextBox;
            TextBox txtContactFName1 = e.Item.FindControl("txtContactFName1") as TextBox;
            TextBox txtContactLName1 = e.Item.FindControl("txtContactLName1") as TextBox;
            TextBox txtContactMInitial1 = e.Item.FindControl("txtContactMInitial1") as TextBox;
            TextBox txtContactEmail1 = e.Item.FindControl("txtContactEmail1") as TextBox;
            TextBox txtContactPhoneNum1 = e.Item.FindControl("txtContactPhoneNum1") as TextBox;
            TextBox txtContactFax1 = e.Item.FindControl("txtContactFax1") as TextBox;
            TextBox txtAddress1 = e.Item.FindControl("txtAddress1") as TextBox;
            TextBox txtAddress2 = e.Item.FindControl("txtAddress2") as TextBox;
            TextBox txtContactFName2 = e.Item.FindControl("txtContactFName2") as TextBox;
            TextBox txtState = e.Item.FindControl("txtState") as TextBox;
            TextBox txtContactLName2 = e.Item.FindControl("txtContactLName2") as TextBox;
            TextBox txtContactMInitial2 = e.Item.FindControl("txtContactMInitial2") as TextBox;
            TextBox txtContactEmail2 = e.Item.FindControl("txtContactEmail2") as TextBox;
            TextBox txtPhnoeNo = e.Item.FindControl("txtPhnoeNo") as TextBox;
            TextBox txtWebSite = e.Item.FindControl("txtWebSite") as TextBox;
            TextBox txtFax = e.Item.FindControl("txtFax") as TextBox;
            TextBox txtEmail = e.Item.FindControl("txtEmail") as TextBox;
            TextBox txtContactPhoneNum2 = e.Item.FindControl("txtContactPhoneNum2") as TextBox;
            TextBox txtContactFax2 = e.Item.FindControl("txtContactEmail2") as TextBox;
            Label lblmessage = e.Item.FindControl("lblmessage") as Label;
            TextBox txtCity = e.Item.FindControl("txtCity") as TextBox;
          
            var VendorUpdate = new DealesDataContext();
          
                var Vendor = VendorUpdate.Update_Vendor(Convert.ToInt32(VendorID), Convert.ToInt32(dropIndustry.SelectedValue), txtName.Text, txtAddress1.Text, txtAddress2.Text,txtCity.Text, txtState.Text, txtZipCode.Text, txtPhnoeNo.Text, txtFax.Text, txtWebSite.Text, txtEmail.Text, Convert.ToBoolean(0), txtContactFName1.Text, txtContactLName1.Text, txtContactMInitial1.Text, txtContactEmail1.Text, txtContactPhoneNum1.Text, txtContactFax1.Text, txtContactFName2.Text, txtContactLName2.Text, txtContactMInitial2.Text, txtContactEmail2.Text, txtContactPhoneNum2.Text, txtContactFax2.Text, Convert.ToInt32(ViewState["VendorContactID1"]), Convert.ToInt32(ViewState["VendorContactID2"]));
            
            Response.Redirect("Vendor.aspx");
        }
        catch(Exception ex)
        {
        }
    }
    protected void RadGrid1_EditCommand(object source, GridCommandEventArgs e)
    {



    }
}

